package com.itye.deom1;

import com.itye.deom1.ClassRoom;
import com.itye.deom1.Group;
import com.itye.deom1.Student;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class RandomPickerFrame extends JFrame {
    private JLabel displayLabel;
    private JButton startButton;
    private JButton stopButton;
    private Timer timer;
    private Random random = new Random();
    private boolean isRunning = false;
    private ClassRoom[] classRooms;
    private int currentMode = 0; // 0:班级, 1:小组, 2:学生

    public RandomPickerFrame(ClassRoom[] classRooms) {
        this.classRooms = classRooms;

        // 设置窗口基本属性
        setTitle("随机抽取系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 创建显示标签
        displayLabel = new JLabel("等待抽取...", SwingConstants.CENTER);
        displayLabel.setFont(new Font("微软雅黑", Font.BOLD, 32));
        displayLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        // 创建模式选择面板
        JPanel modePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JRadioButton classButton = new JRadioButton("抽取班级");
        JRadioButton groupButton = new JRadioButton("抽取小组");
        JRadioButton studentButton = new JRadioButton("抽取学生");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(classButton);
        buttonGroup.add(groupButton);
        buttonGroup.add(studentButton);
        classButton.setSelected(true);

        modePanel.add(classButton);
        modePanel.add(groupButton);
        modePanel.add(studentButton);

        // 创建控制按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        startButton = new JButton("开始");
        stopButton = new JButton("停止");
        stopButton.setEnabled(false);

        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);

        // 添加组件到主面板
        mainPanel.add(modePanel, BorderLayout.NORTH);
        mainPanel.add(displayLabel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 添加主面板到窗口
        add(mainPanel);

        // 创建定时器
        timer = new Timer(50, e -> {
            if (isRunning) {
                switch (currentMode) {
                    case 0 -> displayRandomClass();
                    case 1 -> displayRandomGroup();
                    case 2 -> displayRandomStudent();
                }
            }
        });

        // 添加模式选择事件
        classButton.addActionListener(e -> currentMode = 0);
        groupButton.addActionListener(e -> currentMode = 1);
        studentButton.addActionListener(e -> currentMode = 2);

        // 添加按钮事件
        startButton.addActionListener(e -> {
            isRunning = true;
            timer.start();
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            displayLabel.setForeground(Color.BLACK);
        });

        stopButton.addActionListener(e -> {
            isRunning = false;
            timer.stop();
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            displayLabel.setForeground(Color.RED);
        });
    }

    private void displayRandomClass() {
        if (classRooms.length > 0) {
            ClassRoom randomClass = classRooms[random.nextInt(classRooms.length)];
            displayLabel.setText(randomClass.getClassName());
        }
    }

    private void displayRandomGroup() {
        if (classRooms.length > 0) {
            ClassRoom randomClass = classRooms[random.nextInt(classRooms.length)];
            Group randomGroup = randomClass.getRandomGroup();
            if (randomGroup != null) {
                displayLabel.setText("小组 " + randomGroup.getGroupId());
            }
        }
    }

    private void displayRandomStudent() {
        if (classRooms.length > 0) {
            ClassRoom randomClass = classRooms[random.nextInt(classRooms.length)];
            Student randomStudent = randomClass.getRandomStudentFromClass();
            if (randomStudent != null) {
                displayLabel.setText(randomStudent.getName());
            }
        }
    }

    public static void main(String[] args) {
        // 创建测试数据
        Student student1 = new Student("张三", 202301);
        Student student2 = new Student("李四", 202302);
        Student student3 = new Student("王五", 202303);
        Student student4 = new Student("赵六", 202304);
        Student student5 = new Student("孙七", 202305);
        Student student6 = new Student("周八", 202306);

        Group group1 = new Group(01);
        group1.addStudent(student1);
        group1.addStudent(student2);
        group1.addStudent(student3);

        Group group2 = new Group(02);
        group2.addStudent(student4);
        group2.addStudent(student5);
        group2.addStudent(student6);

        ClassRoom classRoom = new ClassRoom("七班");
        classRoom.addGroup(group1);
        classRoom.addGroup(group2);
        classRoom.addStudent(student1);
        classRoom.addStudent(student2);
        classRoom.addStudent(student3);
        classRoom.addStudent(student4);
        classRoom.addStudent(student5);
        classRoom.addStudent(student6);
        ClassRoom classRoom2 = new ClassRoom("八班");
        classRoom.addGroup(group1);
        classRoom.addGroup(group2);
        classRoom.addStudent(student1);
        classRoom.addStudent(student2);
        classRoom.addStudent(student3);
        classRoom.addStudent(student4);
        classRoom.addStudent(student5);
        classRoom.addStudent(student6);


        ClassRoom[] classRooms = {classRoom ,classRoom2};

        SwingUtilities.invokeLater(() -> {
            RandomPickerFrame frame = new RandomPickerFrame(classRooms);
            frame.setVisible(true);
        });
    }
}